package com.shxc.smarttrade.old.adapternew;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.shxc.smarttrade.R;
import com.shxc.smarttrade.old.bean.MessageBean;
import com.shxc.smarttrade.old.myinterface.RecyclerViewItemClickListener;
import com.shxc.smarttrade.old.utils.BaseUtils;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by jcdev1 on 2017/5/16.
 */

public class MessageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private LayoutInflater mInflater;
    private RecyclerViewItemClickListener listener;
    private List<MessageBean.DataBean> msgList;

    public MessageAdapter(Context mContext) {
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);

    }

    public void setDatas(List<MessageBean.DataBean> msgList) {
        this.msgList = msgList;
        notifyDataSetChanged();
    }

    public void setMyItemCLickListener(RecyclerViewItemClickListener listener) {
        this.listener = listener;

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return new MasterHolder(mInflater.inflate(R.layout.adapter_message_layout, parent, false));

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof MasterHolder) {
            final MessageBean.DataBean db = msgList.get(position);
            if (db.getMsgType() == 0)
                ((MasterHolder) holder).tvMsgType.setText("系统消息");
            else
                ((MasterHolder) holder).tvMsgType.setText("交易消息");

            ((MasterHolder) holder).tvMsgTime.setText(BaseUtils.getDateFromMillionSeconds(db.getCreatedAt(), "yyyy-MM-dd HH:mm:ss"));

            ((MasterHolder) holder).tvMsgContent.setText(db.getMsgInfo());
            if (db.getStatus() == 0)
                ((MasterHolder) holder).tvTag.setVisibility(View.VISIBLE);
            else
                ((MasterHolder) holder).tvTag.setVisibility(View.INVISIBLE);

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onItemCLick(position, db);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return msgList == null ? 0 : msgList.size();
    }


    public class MasterHolder extends RecyclerView.ViewHolder {


        @BindView(R.id.tv_tag)
        TextView tvTag;
        @BindView(R.id.tv_msg_type)
        TextView tvMsgType;
        @BindView(R.id.tv_msg_time)
        TextView tvMsgTime;
        @BindView(R.id.tv_msg_content)
        TextView tvMsgContent;

        MasterHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            AutoUtils.autoSize(view);
        }
    }
}
